import urllib, urllib2, cookielib, re

class Connector(object):
  def __init__(self, user='', passwd=''):
    user = user
    passwd = passwd

    url = 'http://www.mitbbs.com/newindex/mitbbs_bbslogin.php'
    data = [
              ('id',         user),
              ('passwd',     passwd),
              ('kick_multi', '1')]   # confirm login with multi_session.
    cookie_jar = cookielib.CookieJar()
    self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
    
    if user and passwd :   
      response = self.opener.open(url, urllib.urlencode(dict(data)))
      home_page = response.read()
      self.connected = re.search(ur'alert\("\u00D3\u00C3\u00BB\u00A7', home_page)
    else :
      self.connected = False
      
      
  def get_page(self, url):
    page = self.opener.open(url)
    return page.read().decode('gb2312')
    
    
  def isConnected(self):
    return self.connected
      
      